#define DELETE_FROM_RUNQ(t) \
  if(t==runq_head && t==runq_tail) {  \
    runq_head = NULL; \
    runq_tail = NULL; \
  } \
  else if(t==runq_head) { \
    runq_head = runq_head->next; \
    runq_head->previous = NULL; \
  } \
  else if(t==runq_tail) { \
    runq_tail = runq_tail->previous; \
    runq_tail->next = NULL; \
  }  \
  else { \
    t->next->previous = t->previous; \
    t->previous->next = t->next; \
  }

#define DELETE_FROM_WAITQ(t) \
  if(t==waitq_head && t==waitq_tail) {  \
    waitq_head = NULL; \
    waitq_tail = NULL; \
  } \
  else if(t==waitq_head) { \
    waitq_head = waitq_head->next; \
    waitq_head->previous = NULL; \
  } \
  else if(t==waitq_tail) { \
    waitq_tail = waitq_tail->previous; \
    waitq_tail->next = NULL; \
  }  \
  else { \
    t->next->previous = t->previous; \
    t->previous->next = t->next; \
  }

#define ADD_WAITQ(t) \
  if(waitq_head==waitq_tail ) { \
    if (waitq_head!=NULL) { \
      t->previous = waitq_tail; \
      waitq_tail->next = t; \
      waitq_tail = t; \
    } \
    else { \
      waitq_tail = t; \
      waitq_head = waitq_tail; \
    } \
  } \
  else { \
    t->previous = waitq_tail; \
    waitq_tail->next = t; \
    waitq_tail = t; \
  }

#define ADD_RUNQ(t) \
  if(runq_head==runq_tail ) { \
    if (runq_head!=NULL) { \
      t->previous = runq_tail; \
      runq_tail->next = t; \
      runq_tail = t; \
    } \
    else { \
      runq_tail = t; \
      runq_head = runq_tail; \
    } \
  } \
  else { \
    t->previous = runq_tail; \
    runq_tail->next = t; \
    runq_tail = t; \
  }

#define NEW_CURRENT(t) \
  current = t; \
  current->previous = NULL; \
  current->next = NULL; \
  current->status = RUNNING;
  
#define FREE(t) \
  	VALGRIND_STACK_DEREGISTER(t->valgrind_stackid); \
  	if (initialized_library) { \
  		free(t->ctx.uc_stack.ss_sp); \
	  	/*printf("free pile %p\n", t->ctx.uc_stack.ss_sp);*/ \
  		t->ctx.uc_stack.ss_sp=NULL; \
	} \
  	if(t) { \
	  	/*printf("free: %p\n",t );*/\
  		free(t);\
  		t=NULL; \
  	}
  		
